﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TSP
{
    class DistanceGrid
    {
        private double[,] distanceGrid;
        private Dictionary<City, int> cityMap;

        private void PopulateGrid(City[] cities)
        {
            distanceGrid = new double[cities.Length, cities.Length];

            for (int i = 0; i < cities.Length; ++i)
            {
                for (int j = 0; j < cities.Length; ++j)
                {
                    distanceGrid[i, j] = cities[i].costToGetTo(cities[j]);
                }
            }
        }

        private void BuildCityMap(City[] cities)
        {
            cityMap = new Dictionary<City, int>();

            for (int i = 0; i < cities.Length; ++i)
            {
                cityMap.Add(cities[i], i);
            }
        }

        public DistanceGrid(City[] cities)
        {
            distanceGrid = new double[cities.Length, cities.Length];
            PopulateGrid(cities);
            BuildCityMap(cities);
        }

        public double GetDistance(int startCity, int endCity)
        {
            return distanceGrid[startCity, endCity];
        }

        private int GetCityIndex(City city)
        {
            return cityMap[city];
        }

        public double GetDistance(City startCity, City endCity)
        {
            return distanceGrid[startCity.Index, endCity.Index];
        }
    }
}
